package com.king.app

import java.sql.{DriverManager, ResultSetMetaData}
import java.util.Properties

import com.alibaba.fastjson.JSONObject
import org.apache.flink.streaming.api.scala.StreamExecutionEnvironment

import scala.collection.mutable.ListBuffer

/**
 * @Author: KingWang
 * @Date: 2022/1/24  
 * @Desc:
 **/
object PhoenixTest {
  def main(args: Array[String]): Unit = {

    val env = StreamExecutionEnvironment.getExecutionEnvironment

    Class.forName("org.apache.phoenix.jdbc.PhoenixDriver")

    val prop = new Properties()
    prop.setProperty("phoenix.schema.isNamespaceMappingEnabled","true")
    val conn = DriverManager.getConnection("jdbc:phoenix:hadoop200,hadoop201,hadoop202:2181:/hbase",prop)
    val stmt = conn.createStatement();
    val rs = stmt.executeQuery("select * from student ")
    val meta:ResultSetMetaData = rs.getMetaData
    val list = new ListBuffer[JSONObject]
    while(rs.next()){
      val obj = new JSONObject()
     for(i <- 1 to meta.getColumnCount){
       obj.put(meta.getColumnName(i), rs.getObject(i) )
     }
      list.append(obj)
    }

    rs.close()
    stmt.close()
    conn.close()

    list.foreach(x=> println(x.toJSONString))
  }
}
